/**
 * 
 */
package sample.freemarker;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;

/**
 * @author shiwei
 * 
 */
public class FreeMarkerUtils
{
	private static final Log logger = LogFactory.getLog(Process.class);
    public static void generate(File file, Map<String, Object> dataMap) throws Exception
    {
        Configuration cfg = new Configuration();
        Writer out = null;
        try
        {
            if (file.getName().startsWith("tree"))
            {
                if (!dataMap.get("treeflag").equals("1"))
                {
                    return;
                }
            }

            String path = file.getPath();
            String outPath = path
                    .replace(
                            null == dataMap.get("codeType") ? "codetemplate" : ("simple2".equals(dataMap
                                    .get("codeType")) ? "codetemplate2" : dataMap.get("codeType").toString()), "out")
                    .replace("${projectName}", dataMap.get("projectName").toString())
                    .replace("${module}", dataMap.get("module").toString())
                    .replace("${tableName}", dataMap.get("tableName").toString())
                    .replace("${tableNameMin}", dataMap.get("tableNameMin").toString())
                    .replace("${company}", dataMap.get("company").toString());
            if (null != dataMap.get("outPath"))
            {
                outPath = dataMap.get("outPath").toString()
                        + File.separator
                        + outPath.substring(outPath.indexOf(File.separator + "out" + File.separator)
                                + (File.separator + "out" + File.separator).length());

                // System.out.println("代码生成输出路径：" + outPath);
            }
            logger.info("代码生成输出路径：{}" + outPath);
            cfg.setDirectoryForTemplateLoading(file.getParentFile());
            cfg.setDefaultEncoding("UTF-8");
            cfg.setObjectWrapper(new DefaultObjectWrapper());
            cfg.setOutputEncoding("UTF-8");

            Template temp = cfg.getTemplate(file.getName(), "UTF-8");
            File outFile = new File(outPath);

            File tempFile = new File(outFile.getParent());
            if (!tempFile.exists())
            {
                tempFile.mkdirs();
            }
            FileOutputStream fileOut = new FileOutputStream(outFile);
            out = new OutputStreamWriter(fileOut, "UTF-8");
            if(!file.getName().endsWith("pom.xml")){
                temp.process(dataMap, out);;
            }
            out.flush();
        }
        catch (Exception e)
        {
        	logger.error("",e);
        	throw e;
        }
        finally
        {
            if (null != out)
                try
                {
                    out.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
        }
    }
}
